package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IYddUserService yddUserService;
    @Autowired
    private IYddArticleService yddArticleService;
    @Autowired
    private IYddUserDataService yddUserDataService;
    /**
     * 我的文章
     * @param limit
     * @param page
     * @return
     */
    @GetMapping("myArticle")
    private ResultData myArticle(String limit, String page){
        String userId = UserHolder.getUserId();
        //转换
        long currentPage = Long.parseLong(page);
        long pageSize = Long.parseLong(limit);
        //分页查询器
        Page<YddArticle> yddArticlePage = new Page<YddArticle>(currentPage,pageSize);
        //查询构造器
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        //查询条件
        lqw.eq(YddArticle::getUserId,userId);
        //进行查询
        yddArticleService.page(yddArticlePage,lqw);
        //返回结果
        return ResultData.ok("", PageResult.getPage(yddArticlePage));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delArticle")
    private ResultData delArticle(String id){
        //进行一个判断
        if (StringUtils.isNotEmpty(id)) {
            //将String转化为long
            long longId = Long.parseLong(id);
            //调用方法删除
            boolean b =yddArticleService.removeById(longId);
            //对删除结果进行判断
            if (b) {
                return ResultData.ok("删除成功", null);
            }
        }
        return ResultData.error("删除失败");
    }

    /**
     * 修改文章
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public R updateUser(@RequestBody YddUser yddUser){
        yddUserService.updateById(yddUser);
        return R.success("Code",200);
    }

    /**
     * 发布文章
     * @param yddArticle
     * @return
     */
    @PostMapping("/addArticle")
    public R addArticle(@RequestBody YddArticle yddArticle){
        String userId = UserHolder.getUserId();
        log.info("{}",userId);
        yddArticle.setUserId(Integer.parseInt(userId));
        yddArticleService.save(yddArticle);
        return R.success("Code",200);
    }

    /**
     * 点赞收藏,取消
     * @param type       1表示收藏操作，2表示点赞操作
     * @param article_id 文章id
     * @param action     add表示 点赞或收藏， del表示取消点赞或取消收藏
     * @return
     */
    @GetMapping("/userDataHandle")
    public ResultData userDataHandle(String type, String article_id, String action) {
        String userId = UserHolder.getUserId();
        int d = 2;
        if (action.equals("add")) {
            YddUserData yddUserData = new YddUserData();
            yddUserData.setUserId(Integer.valueOf(userId));
            yddUserData.setArticleId(Integer.valueOf(article_id));
            yddUserData.setType(Integer.valueOf(type));

            yddUserDataService.save(yddUserData);
        } else {
            LambdaQueryWrapper<YddUserData> lqw = new LambdaQueryWrapper<>();
            lqw.eq(YddUserData::getUserId, userId).eq(YddUserData::getArticleId, article_id).eq(YddUserData::getType, type);

            yddUserDataService.remove(lqw);
        }
        return ResultData.ok("操作成功", null);
    }

    /**
     * 修改用户
     * @param yddArticle
     * @return
     */
    @PostMapping("/editArticle")
    public ResultData updateArticle(@RequestBody YddArticle yddArticle) {
        yddArticleService.updateById(yddArticle);
        return ResultData.ok("", null);
    }

    /**
     * 查看点赞收藏
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/userDataList")
    public ResultData userLike(int page, int limit,int type) {
        //构造分页构造器

        String userId = UserHolder.getUserId();
        LambdaQueryWrapper<YddUserData> yddUserDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddUserDataLambdaQueryWrapper.eq(YddUserData::getUserId, Integer.parseInt(userId));
        yddUserDataLambdaQueryWrapper.eq(YddUserData::getType, type);
        yddUserDataLambdaQueryWrapper.select(YddUserData::getArticleId);
        List<YddUserData> userDataList = yddUserDataService.list(yddUserDataLambdaQueryWrapper);
        //查询
        List<Integer> articleIds=new ArrayList<>();
        for (YddUserData yddUserData : userDataList) {
            articleIds.add(yddUserData.getArticleId());
        }

        Page<YddArticle> page1 = new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper =  new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.in(YddArticle::getId,articleIds);
        yddArticleService.page(page1,yddArticleLambdaQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("data",page1.getRecords());
        map.put("totalPages", page1.getPages());
        map.put("currentPage", page1.getCurrent());
        map.put("pageSize", page1.getSize());
        map.put("count", page1.getTotal());

        Map<String,Object> data = new HashMap<>();
        data.put("list",map);
        return ResultData.ok("成功",data);

    }

    @GetMapping("/getUserInfo")
    public ResultData getInfo(){
        String userId = UserHolder.getUserId();

        Map<String,Object> data = new HashMap<>();
        //articleNum
        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(YddArticle::getUserId,userId);
        int articleNum = yddArticleService.count(articleLambdaQueryWrapper);
        data.put("articleNum",articleNum);

        //collectNum
        LambdaQueryWrapper<YddUserData> userDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userDataLambdaQueryWrapper.eq(YddUserData::getUserId,userId).eq(YddUserData::getType,1);
        int collectNum = yddUserDataService.count(userDataLambdaQueryWrapper);
        data.put("collectNum",collectNum);

        //userInfo
        YddUser userInfo = yddUserService.getById(userId);
        data.put("userInfo",userInfo);

        //zanNum
        LambdaQueryWrapper<YddUserData> userDataLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        userDataLambdaQueryWrapper1.eq(YddUserData::getUserId,userId).eq(YddUserData::getType,2);
        int zanNum = yddUserDataService.count(userDataLambdaQueryWrapper1);
        data.put("zanNum",zanNum);

        return ResultData.ok("",data);
    }
}

